package main

import "fmt"

type Node struct {
	pass int
	end  int
	next map[byte]*Node
}

var root *Node

func NewNode() *Node {
	return &Node{
		pass: 0,
		end:  0,
		next: make(map[byte]*Node),
	}
}

func (node *Node) insert(word string) {

	if word == "" {
		return
	}
	chars := []byte(word)
	var path byte
	for i := 0; i < len(chars); i++ {
		path = chars[i]
		// 以字符的ASCII码作为路径
		if _, ok := node.next[path]; !ok {
			node.next[path] = NewNode()
		}
		node = node.next[path]
		node.pass++
	}
	node.end++
}

func (node *Node) search(word string) int {
	if word == "" {
		return 0
	}
	chars := []byte(word)
	var newNode = root
	var path byte
	for i := 0; i < len(chars); i++ {
		// 计算路径
		path = chars[i]
		if newNode.next[path] == nil {
			return 0
		}
		newNode = newNode.next[path]
	}
	return newNode.end

}

func main() {
	root = NewNode()
	root.insert("abc")
	root.insert("abdc")
	root.insert("bcd")
	root.insert("bcde")
	fmt.Println(root.search("abcddd"))
}
